/**
 * 
 */
package com.sl.service;

import java.util.concurrent.Future;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import com.sl.entity.User;

/**
 * @author lunshi
 *
 */
@Service
public class GithubLookupService{

	private static final Logger logger = LoggerFactory.getLogger(GithubLookupService.class);
	
	private final RestTemplate restTemplate;

    public GithubLookupService(RestTemplateBuilder restTemplateBuilder) {
        this.restTemplate = restTemplateBuilder.build();
    }
	
    @Async
	public Future<User> findUser(String name) throws InterruptedException{
		logger.info("lookup:"+name);
		String url = String.format("https://api.github.com/users/%s", name);
		User result = restTemplate.getForObject(url, User.class);
		Thread.sleep(1000);
		return new AsyncResult<User>(result);
	}
}
